package kamikaze.entity;

import java.io.Serializable;
import java.math.BigDecimal;
import java.security.Key;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;


public class Payment implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 6864503256695167019L;
	private static final String SHARED_SECRET = "iuowerc12";
	private static final String MECH_ID = "syscanteam2";
	
	private String cardNumber;
	private String cardName;
	private String cardType;
	private BigDecimal amount;
	private String expy;
	private String expm;
	private Date timestamp;
	

	public void setCardName(String cardName) {
		this.cardName = cardName;
	}


	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}
	

	public void setCardNumber(String cardNumber) {
		this.cardNumber = cardNumber;
	}

	public void setCardType(String cardType) {
		this.cardType = cardType;
	}

	public void setTimestamp(Date timestamp) {
		this.timestamp = timestamp;
	}
	
	public void setExpy(String expy) {
		this.expy = expy;
	}

	public void setExpm(String expm) {
		this.expm = expm;
	}
	
	
	public String getPaymentGateway() throws Exception{
		SimpleDateFormat sdfQS = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		StringBuilder sb = new StringBuilder();
		sb.append("http://securecode.syscan.org/pay.php");
		sb.append("?amount=").append(amount);
		sb.append("&cnum=").append(cardNumber);
		sb.append("&expm=").append(expm);
		sb.append("&expy=").append(expy);
		sb.append("&mechid=").append(MECH_ID);
		sb.append("&name=").append(cardName);
		sb.append("&timestamp=").append(sdfQS.format(timestamp));
		sb.append("&type=").append(cardType);
		sb.append("&hash=").append(getHmac());
		
		return sb.toString().replace(" ", "+");
		
	}

	
	public String getHmac() throws Exception {
		SimpleDateFormat sdfHmac = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		
		StringBuilder sb = new StringBuilder();
		sb.append("amount").append(amount);
		sb.append("cnum").append(cardNumber);
		sb.append("expm").append(expm);
		sb.append("expy").append(expy);
		sb.append("mechid").append(MECH_ID);
		sb.append("name").append(cardName);
		sb.append("timestamp").append(sdfHmac.format(timestamp));
		sb.append("type").append(cardType);
		
		return calcHMAC("HmacMD5", sb.toString(), SHARED_SECRET);
	}
	
	
	public String calcHMAC(String algorithm, String hashInput, String privateKey ) throws Exception{
		byte[] keyBytes = privateKey.getBytes();
		Key key = new SecretKeySpec(keyBytes, 0, keyBytes.length, algorithm); 
		Mac mac = Mac.getInstance(algorithm);
		mac.init(key); 
		return byteArrayToHex(mac.doFinal(hashInput.getBytes()));
	}
	
	protected String byteArrayToHex(byte [] a) {
		int hn, ln, cx;
		String hexDigitChars = "0123456789abcdef";
		StringBuilder buf = new StringBuilder(a.length * 2);
		for(cx = 0; cx < a.length; cx++) {
			hn = ((int)(a[cx]) & 0x00ff) / 16;
			ln = ((int)(a[cx]) & 0x000f);
			buf.append(hexDigitChars.charAt(hn));
			buf.append(hexDigitChars.charAt(ln));
		}
		return buf.toString();
	}
	

}
